//
//  FMDBTool.m
//  GoBigMarket
//
//  Created by 李真 on 16/8/26.
//  Copyright © 2016年 legend. All rights reserved.
//

#import "FMDBTool.h"
#import "FMDBModel.h"

#define SQLITE_NAME @"product.sqlite"

@implementation FMDBTool

static FMDatabase *_fmdb;

+ (void)initialize {
    // 执行打开数据库和创建表操作
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:SQLITE_NAME];
    _fmdb = [FMDatabase databaseWithPath:filePath];
    
    [_fmdb open];
    
#pragma mark - 必须先打开数据库才能创建表。。。否则提示数据库没有打开
    [_fmdb executeUpdate:@"CREATE TABLE IF NOT EXISTS t_modals(id INTEGER PRIMARY KEY, name TEXT NOT NULL, age INTEGER NOT NULL, ID_No INTEGER NOT NULL);"];
}
/**
 *  插入数据
 *
 *  @param model 输入数据模型FMDBModel
 *
 *  @return bool值 是否成功
 */

+ (BOOL)insertModel:(FMDBModel *)model{
    
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO product(productId, productName, productFormat, productPrice, productNum) VALUES ('%zd', '%@', '%@', '%f', '%zd');", model.productId, model.productName, model.productFormat, model.productPrice,model.productNum];
    
    return [_fmdb executeUpdate:insertSql];
}

/**
 *  查询数据
 *
 *  @param query 输入查询字段key
 *
 *  @return 返回数组value
 */
+ (NSArray *)queryData:(NSString *)query {

    NSString *fuzzyQuerySql;
    
    if (query == nil) {
        fuzzyQuerySql = @"SELECT * FROM t_modals;"; 
    }else{
       fuzzyQuerySql  = [NSString stringWithFormat:@"SELECT * FROM product WHERE name LIKE '%%%@%%' OR ID_No LIKE '%%%@%%' OR age LIKE '%%%@%%'", query, query,query];
    }
    
    
    NSMutableArray *arrM = [NSMutableArray array];
    FMResultSet *set = [_fmdb executeQuery:fuzzyQuerySql];
    
    while ([set next]) {
        
        NSString *productId = [set stringForColumn:@"productId"];
        NSString *productName = [set stringForColumn:@"productName"];
        NSString *productFormat = [set stringForColumn:@"productFormat"];
        NSString *productPrice = [set stringForColumn:@"productPrice"];
        NSString *productNum = [set stringForColumn:@"productNum"];
        
        FMDBModel *modal = [FMDBModel modalWithProductId:[productId integerValue] andProductName:productName andProductFormat:productFormat andProductPrice:[productPrice floatValue]andProductNum:[productNum integerValue]];
        
        [arrM addObject:modal];
    }
    return arrM;
}
/**
 *  删除数据
 *
 *  @param deleteSql 输入要删除的字段信息key
 *
 *  @return bool值 是否删除成功
 */
+ (BOOL)deleteData:(NSString *)query {
    
    NSString *delSql;
    
    if (query == nil) {
        delSql = @"DELETE FROM t_modals";
    }else{
        delSql = [NSString stringWithFormat:@"DELETE FROM product WHERE name = '%%%@%%' OR ID_No = '%%%@%%' OR age = '%%%@%%'",query,query,query];
    }
    return [_fmdb executeUpdate:delSql];
}
/**
 *  修改数据
 *
 *  @param num修改后的数量，productId商品id
 *
 *  @return bool值 是否删除成功
 */

+ (BOOL)modifyData:(int )num And:(int )productId {
    NSString *changeSql;
    if (changeSql == nil) {
 
    }else{
      changeSql = [NSString stringWithFormat:@"UPDATE product SET ID_No = %%%d%% WHERE name = %%%d%%",num,productId];
        
        return [_fmdb executeUpdate:changeSql];
    }
    
    return NO;
}
/**
 *  关闭数据库
 *
 *  @return BOOL 是否成功关闭数据库
 */

+ (BOOL)sqlClose{
    
    return [_fmdb close];
}



@end
